import java.util.Random;
import java.util.Scanner;

/**
 * 加减法口算练习系统 V0.1
 * 功能：生成50道100以内的加减法算式习题
 */
public class CalculationSystemV01 {
    
    /**
     * 主类入口
     */
    public static void main(String[] args) {
        System.out.println("欢迎使用加减法口算练习系统 V0.1");
        System.out.println("=================================");
        
        // 生成并显示50道习题
        ExerciseGenerator generator = new ExerciseGenerator();
        Exercise[] exercises = generator.generateExercises(50);
        
        ExercisePrinter printer = new ExercisePrinter();
        printer.printExercises(exercises);
        
        System.out.println("\n=================================");
        System.out.println("习题生成完成，请开始答题！");
    }
}

/**
 * 习题类：存储一道算术题及其相关信息
 */
class Exercise {
    private int firstNumber;    // 第一个操作数
    private int secondNumber;   // 第二个操作数
    private char operator;      // 运算符('+'或'-')
    private int answer;         // 正确答案
    
    public Exercise(int firstNumber, int secondNumber, char operator) {
        this.firstNumber = firstNumber;
        this.secondNumber = secondNumber;
        this.operator = operator;
        calculateAnswer();
    }
    
    private void calculateAnswer() {
        if (operator == '+') {
            this.answer = firstNumber + secondNumber;
        } else if (operator == '-') {
            this.answer = firstNumber - secondNumber;
        }
    }
    
    public int getFirstNumber() {
        return firstNumber;
    }
    
    public int getSecondNumber() {
        return secondNumber;
    }
    
    public char getOperator() {
        return operator;
    }
    
    public int getAnswer() {
        return answer;
    }
    
    @Override
    public String toString() {
        return firstNumber + " " + operator + " " + secondNumber + " = ";
    }
}

/**
 * 习题生成器：负责生成指定数量的算术题
 */
class ExerciseGenerator {
    private Random random = new Random();
    private static final int MAX_NUMBER = 100;  // 最大数字限制
    
    /**
     * 生成指定数量的习题
     * @param count 习题数量
     * @return 生成的习题数组
     */
    public Exercise[] generateExercises(int count) {
        Exercise[] exercises = new Exercise[count];
        
        for (int i = 0; i < count; i++) {
            exercises[i] = generateSingleExercise();
        }
        
        return exercises;
    }
    
    /**
     * 生成一道习题
     * @return 生成的习题
     */
    private Exercise generateSingleExercise() {
        // 随机选择运算符
        char operator = random.nextBoolean() ? '+' : '-';
        
        int firstNumber, secondNumber;
        
        if (operator == '+') {
            // 加法：两个数都小于100，和也小于100
            firstNumber = random.nextInt(MAX_NUMBER);
            secondNumber = random.nextInt(MAX_NUMBER - firstNumber);
        } else {
            // 减法：确保结果非负
            secondNumber = random.nextInt(MAX_NUMBER / 2);
            firstNumber = secondNumber + random.nextInt(MAX_NUMBER - secondNumber);
        }
        
        return new Exercise(firstNumber, secondNumber, operator);
    }
}

/**
 * 习题输出器：负责将习题格式化输出
 */
class ExercisePrinter {
    /**
     * 打印所有习题
     * @param exercises 习题数组
     */
    public void printExercises(Exercise[] exercises) {
        // 每行显示5道题
        int exercisesPerLine = 5;
        
        for (int i = 0; i < exercises.length; i++) {
            System.out.printf("(%d) %s\t", (i + 1), exercises[i].toString());
            
            // 每exercisesPerLine道题换行
            if ((i + 1) % exercisesPerLine == 0) {
                System.out.println();
            }
        }
    }
}